package com.swz.dcrm.ui.home;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.swz.dcrm.R;
import com.swz.dcrm.adpter.FragmentViewPagerAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.TodayCount;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.ui.viewmodel.TodayPlanViewModel;
import com.swz.dcrm.util.Constant;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;


public class TodayPlanFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tab)
    SlidingTabLayout slidingTabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;


    private String[] titles;
    private List<Fragment> fragments;
    FragmentViewPagerAdapter fragmentViewPagerAdapter;

    @Inject
    TodayPlanViewModel todayPlanViewModel;

    public static TodayPlanFragment newInstance() {

        return new TodayPlanFragment();
        // Required empty public constructor
    }
    @Override
    public void onLoadRetry() {
        String type= TodayPlanFragmentArgs.fromBundle(getArguments()).getType();
        switch (type){
            case Constant.TODAY_ARRIVE:
                title.setText(getString(R.string.today_arrive_title));
                titles=getResources().getStringArray(R.array.today_arrive_state);
                todayPlanViewModel.getTodayCount(type)
                        .observe(this, todayCountBaseResponse -> {
                            if(todayCountBaseResponse==null){
                                return;
                            }
                            if(todayCountBaseResponse.isSuccess()){
                                titles=getResources().getStringArray(R.array.today_contact_state);
                                titles[0]=titles[0]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getNotFollowCount());
                                titles[1]=titles[1]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getHasFollowCount());
                                titles[2]=titles[2]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getFailFollowCount());
                                if(fragmentViewPagerAdapter!=null){
                                    for(int i=0;i<titles.length;i++){
                                        TextView textView=  slidingTabLayout.getTitleView(i);
                                        textView.setText(titles[i]);
                                    }
                                    return;
                                }
                                fragments=new ArrayList<>();
                                for(int i=1;i<=titles.length;i++){
                                    fragments.add(TodayPlanItemFragment.newInstance(Constant.TODAY_ARRIVE,i));
                                }
                                if(viewPager.getAdapter()==null){
                                    fragmentViewPagerAdapter=new FragmentViewPagerAdapter(getChildFragmentManager(),fragments, Arrays.asList(titles));
                                    viewPager.setAdapter(fragmentViewPagerAdapter);
                                    slidingTabLayout.setViewPager(viewPager);

                                }
                            }
                        });

                break;
            case Constant.TODAY_BOOKING:
                title.setText(getString(R.string.today_booking_title));
                titles=getResources().getStringArray(R.array.today_booking_state);
                todayPlanViewModel.getTodayCount(type)
                        .observe(this, todayCountBaseResponse -> {
                            if(todayCountBaseResponse==null){
                                return;
                            }
                            if(todayCountBaseResponse.isSuccess()){
                                titles=getResources().getStringArray(R.array.today_contact_state);
                                titles[0]=titles[0]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getNotFollowCount());
                                titles[1]=titles[1]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getHasFollowCount());
                                titles[2]=titles[2]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getFailFollowCount());
                                if(fragmentViewPagerAdapter!=null){
                                    for(int i=0;i<titles.length;i++){
                                        TextView textView=  slidingTabLayout.getTitleView(i);
                                        textView.setText(titles[i]);
                                    }
                                    return;
                                }
                                fragments=new ArrayList<>();
                                for(int i=1;i<=titles.length;i++){
                                    fragments.add(TodayPlanItemFragment.newInstance(Constant.TODAY_BOOKING,i));
                                }
                                if(viewPager.getAdapter()==null){

                                    fragmentViewPagerAdapter=new FragmentViewPagerAdapter(getChildFragmentManager(),fragments, Arrays.asList(titles));
                                    viewPager.setAdapter(fragmentViewPagerAdapter);
                                    slidingTabLayout.setViewPager(viewPager);

                                }
                            }
                        });

                break;
            case Constant.TODAY_CONTACT:
                title.setText(getString(R.string.today_contact_title));

                todayPlanViewModel.getTodayCount(type)
                        .observe(this, todayCountBaseResponse -> {
                            if(todayCountBaseResponse==null){
                                return;
                            }
                            if(todayCountBaseResponse.isSuccess()){
                                titles=getResources().getStringArray(R.array.today_contact_state);
                                titles[0]=titles[0]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getNotFollowCount());
                                titles[1]=titles[1]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getHasFollowCount());
                                titles[2]=titles[2]+getString(R.string.parenthesis,todayCountBaseResponse.getData().getFailFollowCount());
                                if(fragmentViewPagerAdapter!=null){
                                  for(int i=0;i<titles.length;i++){
                                      TextView textView=  slidingTabLayout.getTitleView(i);
                                      textView.setText(titles[i]);
                                  }
                                  return;
                                }
                                fragments=new ArrayList<>();
                                for(int i=1;i<=titles.length;i++){
                                    fragments.add(TodayPlanItemFragment.newInstance(Constant.TODAY_CONTACT,i));
                                }
                                fragmentViewPagerAdapter=new FragmentViewPagerAdapter(getChildFragmentManager(),fragments, Arrays.asList(titles));
                                viewPager.setAdapter(fragmentViewPagerAdapter);
                                slidingTabLayout.setViewPager(viewPager);


                            }
                        });

                break;

        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();

    }
    @Override
    public int layoutId() {
        return R.layout.fragment_today_plan;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        viewPager.setOffscreenPageLimit(3);

        return true;

    }

    @OnClick(R.id.iv_back)
    public void backClick(){
        NavHostFragment.findNavController(this).navigateUp();
    }





}
